#ifndef __HQJG_H__
#define __HQJG_H__

#include <stdlib.h>
#include "head.h"
#include "aexgpio.h"

#define USE_HQJG_UDP

#define		HQJG_BEGIN		0xCC
#define		HQJG_END		0xDD

//|CC|TYPE|BTN|DATA|CRC|DD|
//获得起始标志
#define		HQJG_GET_BEGIN(buf)			(buf[0])
//获得荷载数据的长度
#define		HQJG_GET_LEN(buf)			15
#define		HQJG_GET_CMD(buf)			(buf[1])
#define		HQJG_GET_BTN(buf)			(buf[2])		
//获得数据的起始地址
#define		HQJG_GET_DATA(buf)			(&buf[3])
//获得信令CRC值
#define		HQJG_GET_CRC(buf)			(buf[18])
//获得结束标志
#define		HQJG_GET_END(buf)			(buf[19])
//获得信令参数的长度
#define		HQJG_GET_PACKET_LEN(buf)	(20)		

#define		HQJG_SET_BEGIN(buf)			{buf[0]=HQJG_BEGIN;}
#define		HQJG_SET_LEN(buf,val)		{;}			//设置荷载数据的长度
#define		HQJG_SET_CMD(buf,val)		{buf[1]=val&0xFF;}
#define		HQJG_SET_BTN(buf,val)		{buf[2]=val&0xFF;}		
#define		HQJG_SET_CRC(buf)			{buf[18]=crc8(buf+3,HQJG_GET_LEN(buf));}		//获得数据的CRC值
#define		HQJG_SET_END(buf)			{buf[19]=HQJG_END;}

//===========================================================
#define		HQJG_CMD_BEGIN		0xAA
#define		HQJG_CMD_END		0xBB
//|CC|TYPE|BTN|DATA|CRC|DD|
//获得起始标志
#define		HQJG_CMD_GET_BEGIN(buf)			(buf[0])
//获得荷载数据的长度
#define		HQJG_CMD_GET_LEN(buf)			15
#define		HQJG_CMD_GET_CMD(buf)			(buf[1])
#define		HQJG_CMD_GET_BTN(buf)			(buf[2])		
//获得数据的起始地址
#define		HQJG_CMD_GET_DATA(buf)			(&buf[3])
//获得信令CRC值
#define		HQJG_CMD_GET_CRC(buf)			(buf[18])
//获得结束标志
#define		HQJG_CMD_GET_END(buf)			(buf[19])
//获得信令参数的长度
#define		HQJG_CMD_GET_PACKET_LEN(buf)	(20)		

#define		HQJG_CMD_SET_BEGIN(buf)			{buf[0]=HQJG_CMD_BEGIN;}
#define		HQJG_CMD_SET_LEN(buf,val)		{;}			//设置荷载数据的长度
#define		HQJG_CMD_SET_CMD(buf,val)		{buf[1]=val&0xFF;}
#define		HQJG_CMD_SET_BTN(buf,val)		{buf[2]=val&0xFF;}		
#define		HQJG_CMD_SET_CRC(buf)			{buf[18]=crc8(buf+3,HQJG_CMD_GET_LEN(buf));}		//获得数据的CRC值
#define		HQJG_CMD_SET_END(buf)			{buf[19]=HQJG_CMD_END;}

#ifdef USE_HQJG_UDP

typedef enum{
    CMD_HQJD_ALLON = 0X0A,          //命令全开
    CMD_HQJD_ALLOFF = 0X0B,         //命令全关
    CMD_HQJD_GET_STA = 0X0C,        //查询状态
    CMD_HQJD_SET_TIME = 0X1A,       //设置时间
    CMD_HQJD_GET_TIME = 0X1B,       //查询时间
    CMD_HQJD_SINGLE_ON = 0X0D,      //设置按钮开,单开
    CMD_HQJD_SINGLE_OFF = 0X0E,     //设置按钮关，单关
    CMD_HQJD_AUTO_HEART = 0X1E,     //每隔 7 秒主动上抛设备心跳包
    CMD_HQJD_REQ_STA = 0X2B,        //查询 IO 状态
    CMD_HQJD_INPUT_CTRL_EN = 0X4A,  //IO 按键输入控制功能打开,*此指令需有掉电保存功能。*
    CMD_HQJD_INPUT_CTRL_DIS = 0X4B, //IO 按键输入控制功能关闭,*此指令需有掉电保存功能。*
    CMD_HQJD_MUL_ON_NOW = 0X2D,     //设置按钮即时开
    CMD_HQJD_MUL_OFF_NOW = 0X2E,    //设置按钮即时关
    CMD_HQJD_MUL_ON_DELAY = 0X3D,   //设置按钮延时开，每个IO的设置间隔600ms
    CMD_HQJD_MUL_OFF_DELAY = 0X3E,  //设置按钮延时关，每个IO的设置间隔600ms
    CMD_HQJD_PLAN_ON = 0X2C,        //设置工作日定时开,*此指令需有掉电保存功能，一天中每一线路只分别有一次定时开关。*
    CMD_HQJD_PLAN_OFF = 0X3C,       //设置工作日定时关,*此指令需有掉电保存功能，一天中每一线路只分别有一次定时开关。*
}LIST_CMD_HQJD;


typedef struct {
	u8 operate;
	u32 flag_pins;
}delay_onOff_data;  //延时开关数据结构体

typedef enum{
    HQJD_HOUR_1,
    HQJD_MIN_1,
    HQJD_HOUR_2,
    HQJD_MIN_2,
    HQJD_HOUR_3,
    HQJD_MIN_3,
    HQJD_HOUR_4,
    HQJD_MIN_4,
    HQJD_HOUR_5,
    HQJD_MIN_5,
    HQJD_HOUR_6,
    HQJD_MIN_6,
    HQJD_HOUR_7,
    HQJD_MIN_7,
    HQJD_TIME_NUM,
}HQJD_PLAN_TABLE;

typedef enum{
    HQJD_PLAN_ON,
    HQJD_PLAN_OFF,
    HQJD_PLAN_NUM,
}TYPE_PLAN;

typedef struct
{
    struct itimerval timers[8][HQJD_PLAN_NUM];
    /*
    定时器运行标志
    高位off，低位on
    0x8000,off_ch7
    0x0100,off_ch0
    0x0080,on_ch7
    ox0001,off_ch0
    */
    u16 flags_timerval; 
}sys_plan_timers;

void hqjg_cmd(unsigned char *buf,size_t len);
void hqjd_handle_uevent(unsigned int oldPin, unsigned int newPin);
void hqjd_handle_SIGALRM(void);
void hqjg_report_iostatus(unsigned char cmd,unsigned char btn);
#endif

#endif //__HQJG_H__